package com.example.sms_emoticon_library;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.MapFragment;

import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class SendMapActivity extends Activity implements OnClickListener {
	GoogleMap googleMap;
	TextView tvSave, tvBack;
	double latitude,longitude;
	Intent mIntentFuntion;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_send_map);
		initilizeMap();
		
		mIntentFuntion = getIntent();
		
		tvSave = (TextView) findViewById(R.id.TxtMapSave);
		tvBack= (TextView) findViewById(R.id.TxtMapBack);
		
		tvSave.setOnClickListener(this);
		tvBack.setOnClickListener(this);
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.send_map, menu);
		return true;
	}
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initilizeMap();
	}

	private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
            
            googleMap.setMyLocationEnabled(true);
    		googleMap.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
    			
    			

				@Override
				public void onMyLocationChange(Location arg0) {
					// TODO Auto-generated method stub
					latitude = arg0.getLatitude();
    				longitude = arg0.getLongitude();
    				LatLng latLg= new LatLng(latitude, longitude);
					
    				googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLg));
    				googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
				}
    		});
 
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
        
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.TxtMapBack){
			finish();
		}else if(v.getId()==R.id.TxtMapSave){
			
			Bundle mBundle= new Bundle();
			mBundle.putDouble("longitude", longitude);
			mBundle.putDouble("latitude", latitude);
			mIntentFuntion.putExtra("DataMap", mBundle);
			setResult(FragmentFuntions.RESULT_SEND_MAP,mIntentFuntion );
			finish();
		}
	}

}
